package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Goods;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IImgsService;
import com.woniu.supermarket.vo.GoodsVo;
import com.woniu.supermarket.vo.ImgsGoodsDataVo;
import com.woniu.supermarket.vo.ImgsGoodsVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("imgs")
@Api(value = "图片库操作")
@Slf4j
public class ImgsController {

    @Autowired
    IImgsService iImgsService;




    @Value("${supermarket-goods-img}")
    private String path;


    @PostMapping("/addImgGoods")
    @ApiOperation(value = "添加图片库")
    public ResultResponse<Void> addImgsGoods(@ApiParam(name = "ImgsGoodsVo",value = "接收图片库对象")@RequestBody ImgsGoodsVo imgsGoodsVo){
        ResultResponse<Void> result = null;
        try {
            System.out.println("======================================================");
            System.out.println(imgsGoodsVo);
            int i = iImgsService.add(imgsGoodsVo);

            result = new ResultResponse<>(200,"添加图片库成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加图片库失败");
        }
        return result;
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "删除图片库")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "图片库id", dataType = "int", paramType = "path", name = "id"),
    })
    public ResultResponse<Void> del(@PathVariable("id") int id){
        ResultResponse<Void> result = null;
        try {
            int i = iImgsService.remove(id);
            result = new ResultResponse<>(200,"删除图片库成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除图片库失败");
        }
        return result;
    }

    @DeleteMapping("/delBatch")
    @ApiOperation(value = "批量删除图片库")
    public ResultResponse<Void> delBatch(@RequestParam("ids") int[] ids){
        ResultResponse<Void> result = null;
        try{
            int i = iImgsService.removeBatchByIds(ids);
            result = new ResultResponse<>(200,"批量删除商品成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"批量删除商品失败");
        }
        return result;
    }




    @PutMapping("/modifyImgGoods")
    @ApiOperation(value = "修改图片库")
    public ResultResponse<Void> modify(@ApiParam(name="ImgsGoodsVo",value="接收图片库对象")@RequestBody ImgsGoodsVo imgsGoodsVo){
        ResultResponse<Void> result = null;
        try {
            int i = iImgsService.modify(imgsGoodsVo);
            result = new ResultResponse<>(200,"修改图片库成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改图片库失败");
        }
        return result;
    }



    @GetMapping("/find/{id}")
    @ApiOperation(value = "通过id查图片库")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "图片库id", dataType = "int", paramType = "path", name = "id"),
    })
    public ResultResponse<ImgsGoodsVo> queryByKey(@PathVariable("id") int id){
        ResultResponse<ImgsGoodsVo> result = null;
        try {
            ImgsGoodsVo imgsGoodsVo = iImgsService.queryById(id);
            result = new ResultResponse<>(200,"查找图片库成功",imgsGoodsVo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查找图片库失败");
        }
        return result;
    }

    @GetMapping("/showImgs")
    @ApiOperation(value = "商品分页查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", dataType = "int", paramType = "query", name = "now"),
            @ApiImplicitParam(value = "分页单位", dataType = "int", paramType = "query", name = "size"),
            @ApiImplicitParam(value = "按照描述模糊查询条件", dataType = "String", paramType = "query", name = "des"),
            @ApiImplicitParam(value = "按照类型查询条件", dataType = "String", paramType = "query", name = "typeId"),
    })
    public ResultResponse<PageInfo<ImgsGoodsVo>> showImgs(@RequestParam(value = "now", defaultValue = "1") Integer now,
                                                          @RequestParam(value = "size", defaultValue = "3") Integer size,
                                                          String des,
                                                          @RequestParam(value = "typeId",defaultValue = "0")Integer typeId){
        ResultResponse<PageInfo<ImgsGoodsVo>> result = null;
        try {
            System.out.println(des);
            System.out.println(typeId);
            PageInfo<ImgsGoodsVo> pageInfo = iImgsService.queryByPage(now, size, des, typeId);

            result = new ResultResponse<>(200,"查询图片库成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询图片库失败");
        }
        return result;
    }

    @PostMapping("/upload")
    public ResultResponse<String> upload(@RequestParam("GoodsImg") MultipartFile multipartFile) throws IOException {
        ResultResponse<String> result =  null;
        //把图片上传的位置获取到指定文件夹
        if(multipartFile.getSize()>0){
            //上传
            //获取图片名称
            String filename = multipartFile.getOriginalFilename();
            log.info("path"+path);
            log.info("接收到上传文件的名字是--->"+filename);
            filename = UUID.randomUUID().toString().replaceAll("-","")+filename.substring(filename.lastIndexOf("."));

            File file = new File(path);
            if(!file.exists()){
                file.mkdirs();
            }
            //创建上传的文件
            File f = new File(file,filename);

            //上传
            multipartFile.transferTo(f);

            result = new ResultResponse<>(200, "上传成功！", filename);
        }else{
            //没有上传
            result = new ResultResponse<>(201, "上传失败！");
        }
        return result;
    }


    @GetMapping("/queryImgsGoodVo")
    @ApiOperation(value = "商品分页查询操作")
    public ResultResponse<List<ImgsGoodsDataVo>> queryImgsGoodVo(){
        ResultResponse<List<ImgsGoodsDataVo>> result = null;
        try {
            List<ImgsGoodsDataVo> imgsGoodsDataVos = iImgsService.queryIGDV();

            result = new ResultResponse<List<ImgsGoodsDataVo>>(200,"查询图片库成功",imgsGoodsDataVos);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<List<ImgsGoodsDataVo>>(201,"查询图片库失败");
        }
        return result;
    }

    @PostMapping("/addImgVo")
    @ApiOperation(value = "给商品绑定图片库")
    public ResultResponse<Void> addImgVo(@ApiParam(name = "imgVo",value = "接收图片库中间对象")@RequestBody ImgsGoodsDataVo ImgsGoodsDataVo){
        ResultResponse<Void> result = null;
        try {
            System.out.println("======================================================");
            System.out.println(ImgsGoodsDataVo);

            iImgsService.addIGDV(ImgsGoodsDataVo);
            result = new ResultResponse<>(200,"商品添加图片库成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"商品添加图片库失败");
        }
        return result;
    }


    @DeleteMapping("/delImgGood/{gid}")
    @ApiOperation(value = "删除图片库")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品id", dataType = "int", paramType = "path", name = "gid"),
    })
    public ResultResponse<Void> delImgGood(@PathVariable("gid") int gid){
        ResultResponse<Void> result = null;
        try {
            iImgsService.removeIGDV(gid);
            result = new ResultResponse<>(200,"删除图片库成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除图片库失败");
        }
        return result;
    }


}
